/*******************************************************************************
 * Package: com.song.boot.springstudy.webflux
 * Type:    Cropping
 * Date:    2025-04-06 17:16
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.webflux;

import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.Arrays;

/**
 * 功能描述：  裁剪流中元素
 *
 * @author Songxianyang
 * @date 2025-04-06 17:16
 */
public class Cropping {
    public static void main(String[] args) {
        //检查序列中是否包含偶数：
        Flux.just(1,2,3,4,5,6)
                .any(item -> item % 2 == 0)
                .subscribe(System.out::println);

        Flux.just(1,7,3,4,5,6)
                .doOnNext(item -> System.out.println(item))
                .any(item -> item % 2 == 0)
                .subscribe(System.out::println);
        System.out.println("-------------");
        Flux.range(1,5)
                .reduce(0,(item1,item2)->{
                    System.out.println("item1:" + item1);
                    System.out.println("item2:" + item2);
                    return item1 + item2;
                }).subscribe(System.out::println);
        System.out.println("-------------");

        Flux.range(1,5)
                .scan(0,(num1,num2) ->{
                    System.out.println("num1:" + num1);
                    System.out.println("num2:" + num2);
                    return num1 + num2;
                })
                .subscribe(System.out::println);
        System.out.println(Duration.ofMillis(100));

        int arrLength = 5;
        Flux.just(1,2,3,4,5,6)
                .index()
                .scan(new int[arrLength],(arr,entry) ->{
                    arr[(int) (entry.getT1() % arrLength)] = entry.getT2();
                    return arr;
                }).skip(arrLength)// 当窗口数组背灌满之后，开始计算平均值，因此要跳过没有灌满的情况
                .map(array -> Arrays.stream(array).sum() * 1.0 /arrLength)
                .subscribe(System.out::println);



        Flux.just(1,2,3,4)
                // .doOnNext(item -> System.out.println("副作用：" + item
                // ))
                .thenMany(Flux.just(5,6,7))
                .subscribe(System.out::println);


    }
}
